using System.Collections.Generic;
using UnityEngine;

namespace ZFramework.Runtime
{
    public class ItemGroup : MonoBehaviour
    {
        private string _name;
        private LinkedList<Item> _items;
        private float _instanceAutoReleaseInterval;
        private int _instanceCapacity;
        private float _instanceExpireTime;
        private int _instancePriority;
        private ObjectPoolMgr.ObjectPool<ItemInstanceObject> _instancePool;
        private ObjectPoolMgr _objectPoolMgr;

        public string Name => _name;
        public GameObject Root => gameObject;

        public int ItemCount
        {
            get { return _items.Count; }
        }

        public float InstanceAutoReleaseInterval { get; set; }
        public int InstanceCapacity { get; set; }
        public float InstanceExpireTime { get; set; }
        public int InstancePriority { get; set; }

        public void Init(string name, float instanceAutoReleaseInterval, int instanceCapacity, float instanceExpireTime,
            int instancePriority, ObjectPoolMgr objectPoolMgr)
        {
            _name = name;
            _instanceAutoReleaseInterval = instanceAutoReleaseInterval;
            _instanceCapacity = instanceCapacity;
            _instanceExpireTime = instanceExpireTime;
            _instancePriority = instancePriority;
            _items = new LinkedList<Item>();
            _objectPoolMgr = objectPoolMgr;
            _instancePool = _objectPoolMgr.CreateSingleSpawnObjectPool<ItemInstanceObject>(name,
                instanceAutoReleaseInterval,
                instanceCapacity, instanceExpireTime);
        }

        public void DeInit()
        {

        }

        public void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            foreach (var item in _items)
            {
                item.OnUpdate(elapseSeconds, realElapseSeconds);
            }
        }


        public bool HasItem(int itemId)
        {
            foreach (var item in _items)
            {
                if (item.Id == itemId)
                    return true;
            }

            return false;
        }

        public bool HasItem(string assetPath)
        {
            foreach (var item in _items)
            {
                if (item.AssetPath == assetPath)
                    return true;
            }

            return false;
        }

        public Item GetItem(int itemId)
        {
            foreach (var item in _items)
            {
                if (item.Id == itemId)
                    return item;
            }

            return null;
        }

        public Item GetItem(string assetPath)
        {
            foreach (var item in _items)
            {
                if (item.AssetPath == assetPath)
                    return item;
            }

            return null;
        }

        public Item[] GetItems(string assetPath)
        {
            List<Item> results = new List<Item>();
            foreach (Item item in _items)
            {
                if (item.AssetPath == assetPath)
                {
                    results.Add(item);
                }
            }

            return results.ToArray();
        }

        public void GetItems(string assetPath, List<Item> results)
        {
            results.Clear();
            foreach (Item item in _items)
            {
                if (item.AssetPath == assetPath)
                {
                    results.Add(item);
                }
            }
        }

        public Item[] GetAllItems()
        {
            List<Item> results = new List<Item>();
            foreach (Item item in _items)
            {
                results.Add(item);
            }

            return results.ToArray();
        }

        public void GetAllItems(List<Item> results)
        {
            results.Clear();
            foreach (Item item in _items)
            {
                results.Add(item);
            }
        }

        public void AddItem(Item item)
        {
            _items.AddLast(item);
        }

        public void RemoveItem(Item item)
        {
            if (!_items.Remove(item))
            {
                return;
            }
        }

        //注册一个entityObject
        public void RegisterItemInstanceObject(ItemInstanceObject obj, bool spawned)
        {
            _instancePool.Register(obj, spawned);
        }

        //从池子里拿出一个
        public ItemInstanceObject SpawnItemInstanceObject(string name)
        {
            return _instancePool.Spawn(name);
        }

        public void UnSpawnItem(Item item)
        {
            _instancePool.UnSpawn(item.Obj);
        }
    }
}
